//
//  GoogleAPIToolBox.m
//  iWeather
//
//  Created by Quan Lam on 11/15/13.
//  Copyright (c) 2013 Vo Thi My Hanh (FSU1.BU19). All rights reserved.
//

#import "GoogleAPIToolBox.h"


@implementation GoogleAPIToolBox

static GoogleAPIToolBox* myToolBox = NULL;
CLLocationManager *manager_;

+(GoogleAPIToolBox*) googleAPIToolBox {
    if (!myToolBox) {
        myToolBox = [[GoogleAPIToolBox alloc] init];
        myToolBox.myCaller = [[GoogleAPICaller alloc] init];
        myToolBox.myGeocoder = [[GMSGeocoder alloc] init];
        myToolBox.myAPIService = [[APIService alloc] init];
        myToolBox.myCaller.callerDelegate = myToolBox;
    }
    return myToolBox;
}

+(void) setCallerDelegate:(id<GoogleAPIDelegate>)delegate {
    [self googleAPIToolBox].myCallerDelegate = delegate;
}

+(void) setMyLocationDelegate:(id<GoogleAPIUpdateLocationDelegate>)delegate {
    [self googleAPIToolBox].myLocationDelegate = delegate;
}

- (void) directionsAPICallfrom: (NSString*)fromPOI to: (NSString*)toPOI {
    [self.myCaller directionsAPICallfrom:fromPOI to:toPOI];
}

- (void)directionsAPIRequestFinished:(NSDictionary *)data {
    [self.myCallerDelegate directionsAPIRequestFinished:data];
}

-(void)serviceRequestTimeOut {
    NSLog(@"damn you");
}

-(void)getCurrentLocation {
    // Setup location services
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"Please enable location services");
        return;
    }
    
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
        NSLog(@"Please authorize location services");
        return;
    }
    
    manager_ = [[CLLocationManager alloc] init];
    manager_.delegate = self;
    manager_.desiredAccuracy = kCLLocationAccuracyBest;
    manager_.distanceFilter = 5.0f;
    [manager_ startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
        NSLog(@"Please authorize location services");
        return;
    }
    
    NSLog(@"CLLocationManager error: %@", error.description);
    return;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    [self updateLocationRequestFinished:locations];
}

-(void) updateLocationRequestFinished:(NSArray *)locations {
    [self.myLocationDelegate updateLocationRequestFinished:locations];
}

- (void)reverseGeocode:(CLLocationCoordinate2D)coordinate withDelegate:(id<GoogleAPIReverseGeocodeDelegate>) object {
    // On a long press, reverse geocode this location.
    GMSReverseGeocodeCallback handler = ^(GMSReverseGeocodeResponse *response,
                                          NSError *error) {
        if (response && response.firstResult) {
            [object reverseGeocodeRequestFinished:response];
        } else {
            NSLog(@"Could not reverse geocode point (%f,%f): %@",
                  coordinate.latitude, coordinate.longitude, error);
        }
    };
    [self.myGeocoder reverseGeocodeCoordinate:coordinate
                      completionHandler:handler];
    
}

-(void)requestAllTrafficDataForDelegate:(id<trafficAPIDelegate>) delegate {
    [self.myAPIService setDelegate:delegate];
    [self.myAPIService requestDataWithTypeOfService:kTrafficStuffAPI];
}
-(void) dealloc {
    [manager_ release];
    [self.myCaller release];
    [self.myGeocoder release];
    [super dealloc];
}
@end
